/**
 * Auth controller js code
 */
define(function (require, exports, module) {
    
    require("ajaxproxy");
    require("layer");
    var common = require('common');
    var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
   

    exports.init = function (configs) {

        //添加数据
        $('[data-act=do_add]').on("click", function () {
            if($(this).hasClass("grayBtn")){
                return;
            }
            var braVal = $('.braInput').val();
            if(braVal.length >10){
                layer.msg("牌号请控制在10字符以内");
                return false;
            }
            doSumbit();
            var data = $("#addBrandFrom").serialize();
            var action = $("#addBrandFrom").attr('action');
            $.post(action, data, function (result) {
                var ajax = $.parseJSON(result);
                if (ajax.code == "000") {
                    layer.msg(ajax.message,{shift: -1}, function() {
                        window.location.href='/mobadmin/brand/index';
                    });
                }else if (ajax.code == "002"){

                    layer.msg(ajax.message,{shift: -1}, function() {

                       window.location.href=ajax.data;
                    });
                } else {
                    layer.msg("提交失败");
                }
            });
        });
    };
    /*
    
    
    $(document).on('blur', '.serchInp', function () {
        if ($.trim($(".serchInp").val()) == "") {
            $(".searchBtn").hide();
            $(".searchPop").show();
        }
    });*/
    function panelClose() {
        //$(".masker").hide();
        $(".tpfb_panel").addClass("slideOutRight").one(animationEnd, function() {
            $(this).removeClass('braPanelBounceIn slideOutRight');
        });
    }

    $(".panelClose,.cd_cancel,.masker").click(function () {
        panelClose();
    });


    function doSumbit() {

        $('input[name="data[typeid]"]').val($('.typInput').attr('data-id'));
        $('input[name="data[brandid]"]').val($('.proInput').attr('data-id'));
        $('input[name="data[facid]"]').val($('.facInput').attr('data-id'));
        $('input[name="data[name]"]').val($('.braInput').val());


    }

    function selectRowYes(){
        
        var select_obj = $(this).parents('.tpfb_panel').find('.selectOn').find('.selectTxt');
        var select_obj_name = $(this).parents('.tpfb_panel').find('.selectOn').attr('name');
        if (select_obj_name == 'typ') {
            cleanInput('pro');
            cleanInput('fac');
            cleanInput('bra');
        } else if (select_obj_name == 'pro') {
            cleanInput('fac');
            cleanInput('bra');
        } else if (select_obj_name == 'fac') {
            cleanInput('bra');
        }
        var input_name = select_obj_name + 'Input';
        $("." + input_name).val(select_obj.html());
        $("." + input_name).attr('data-id', select_obj.attr('value'));
        
    }
        


   
    function cleanInput(name) {

        var input_name = name + 'Input';

        $("." + input_name).val('');

        $("." + input_name).attr('data-id', '');
    }

    function selectRowFun(){

        if ($(this).attr("name")) {
            $(".selectRow[name=" + $(this).attr("name") + "]").removeClass("selectOn").find(".selectInpEl").val("off");
        }

        $(this).addClass("selectOn");
        $(this).find(".selectInpEl").val("on");
        panelClose();
        selectRowYes.apply(this);
    };

/*
    $(document).on('click',".searchBtn", function () {
        alert(1);
        var name   =$(this).parents('.tpfb_panel').find('.selectOn').attr('name');

        var keyword=$('.serchInp').val();

        alert(name+" , "+keyword);

        setSearchData(name,keyword);
    });
*/


    $(document).on('click', '.tpfb_input', function () {
       
        if ($(this).hasClass('typInput')) {

            setSearchData('typ');
             $(".typPanel").addClass("slideInRight").one(animationEnd, function() {
                $(this).addClass("braPanelBounceIn").removeClass('slideInRight');
            });
        }

        if ($(this).hasClass('proInput')) {
            setSearchData('pro','');
            $(".proPanel").addClass("slideInRight").one(animationEnd, function() {
                $(this).addClass("braPanelBounceIn").removeClass('slideInRight');
            });
        }
        if ($(this).hasClass('facInput')) {
            setSearchData('fac','');
            $(".facPanel").addClass("slideInRight").one(animationEnd, function() {
                $(this).addClass("braPanelBounceIn").removeClass('slideInRight');
            });
        }
        if (!$(this).hasClass('braInput')) {
            //$('.masker').toggle();
            //$(this).parent().find('.tpfb_panel').toggle();


        }
    });

    function setSearchData(name,keyword){

        var typ = $(".typContent");
        var pro = $(".proContent");
        var fac = $(".facContent");
        var bra = $(".braContent");

        var typId = $('.typInput').attr('data-id');
        var proId = $('.proInput').attr('data-id');
        var facId = $('.facInput').attr('data-id');
        var braId = $('.braInput').attr('data-id');

        if (name=='typ') {
            getTypeList(function (data) {
                setValueInSelect(typ, "请选择品类", "typ", "name", data)
            });
        }
        if (name=='pro') {
            getNameList(typId,keyword, function (data) {
                setValueInSelect(pro, "请选择品名", "pro", "name", data)
            });
        }
        if (name=='fac') {
            getFactoryList(typId, proId,keyword, function (data) {
                setValueInSelect(fac, "请选择厂家", "fac", "name", data)
            });
        }

    }
    /* start 分类品名厂家牌号四级联动所用的*/
    /**
     * 返回所有产品分类列表
     */

    function getTypeList(callback) {
        var url = "/brand/brand/type?timestamp=" + new Date().getTime();
        $.getJSON(url, function (data) {
            callback(data);
        });
    }

    /**
     * 通过产品分类ID取得品名列表
     * @param typeId 分类Id
     */
    function getNameList(typeId,name, callback) {
        var url = "/brand/brand/name?type_id=" + typeId + "&name=" + name + "&timestamp=" + new Date().getTime();
        $.getJSON(url, function (data) {

            callback(data);

        });
    }

    /**
     * 品名ID取得厂家列表
     * @param typeId 分类Id
     * @param productId 品名Id
     */
    function getFactoryList(typeId, productId, name,callback) {
        var url = "/brand/brand/factory?type_id=" + typeId + "&product_id=" + productId + "&name=" + name + "&timestamp=" + new Date().getTime();
        $.getJSON(url, function (data) {

            callback(data);
        });
    }

    /**
     * 按分类ID，品名ID,厂家取得牌号列表
     * @param typeId 分类Id
     * @param productId 品名Id
     * @param factoryId 品名Id
     */
    function getBrandList(typeId, productId, factoryId, callback) {
        factoryId = encodeURI(factoryId);
        var url = "/brand/brand/brand?timestamp=" + new Date().getTime() + "&type_id=" + typeId + "&product_id=" + productId + "&factory_id=" + factoryId;
        $.getJSON(url, function (data) {
            callback(data);
        });
    }
    /**
    *   生成selectRow的html，返回生成的html字符串
    *   
    *
    *
    **/
    function generateHtml(optionValueParamName,data){
        var s= "";
        if(optionValueParamName=='pro'){
                
                $(data).each(function (index, el) {

                    var selectInpEl_value = 'off';
                    var selectRow_class = '';

                    s = s + '<div class="zimuRow">' + el.lt + '</div>';


                    $(el.lt_items).each(function (index2, el2) {

                        if (index==0&&index2 == 0) {
                            selectInpEl_value = 'on';
                            selectRow_class = 'selectOn';
                        }else{
                            selectInpEl_value = 'off';
                            selectRow_class = '';

                        }
                        s = s + '<div class="selectRow ' + selectRow_class + '" name="' + optionValueParamName + '">' +
                        '<span class="selectTxt" value="' + el2.id + '">' + el2.name + '</span>' +
                        '<span class="radioBtn"><span class="radioCore"></span></span>' +
                        '<input type="hidden" class="selectInpEl" value="' + selectInpEl_value + '"/>' +
                        '</div>';
                    });

                });

            }else{
                $(data).each(function (index, el) {

                    var selectInpEl_value = 'off';
                    var selectRow_class = '';

                    if (index == 0) {
                        selectInpEl_value = 'on';
                        selectRow_class = 'selectOn';
                    }
                    s = s + '<div class="selectRow ' + selectRow_class + '" name="' + optionValueParamName + '">' +
                        '<span class="selectTxt" value="' + el.id + '">' + el.name + '</span>' +
                        '<span class="radioBtn"><span class="radioCore"></span></span>' +
                        '<input type="hidden" class="selectInpEl" value="' + selectInpEl_value + '"/>' +
                        '</div>';


                });
            }
        return s;
    }

    function setValueInSelect(JQselector, firstOption, optionValueParamName, optionHtmlParamName, data, defaultOption) {

        //JQselector.html("<span >" + firstOption + "</span>");

        JQselector.html(function () {
            var s='';

            if(optionValueParamName=='pro'||optionValueParamName=='fac'){

                 s = '<div class="searchRow" name="' + optionValueParamName + '">'+
                    '<input class="serchInp" />'+
                    '<a href="javascript:void(0)" class="searchBtn">搜索</a>'+
                    '<div class="searchPop">'+
                    '<span class="searchIcon"></span> <span class="searchPsTxt">搜索</span>'+
                    '</div>'+
                    '</div>'+
                    '<div class="blank30"></div>';
            }
            s = s +"<div id='contentCol" + optionValueParamName + "' >";
            s=s+generateHtml(optionValueParamName,data);
            s=s+"</div>"

            return s;
        });
        
        
        
        if (defaultOption) {
            JQselector.find("[value='" + defaultOption + "']").attr("selected", "selected");
            if (optionHtmlParamName == 'brandNumber') {
                var bra_id = $("input[name='brand_id']");

                bra_id.val(JQselector.find("option:selected").attr('data-brand-id'));
            }
        }
        
        JQselector.find(".searchBtn").click(function(){
           
            var name =$(this).parents('.tpfb_panel').find('.selectOn').attr('name');
            var keyword=JQselector.find('.serchInp').val(); 
          
            setSearchData(name,keyword);
        });
        JQselector.find(".selectRow").click(function(){
            selectRowFun.apply(this);
        });
        
        JQselector.find(".searchPop").click(function(){
            JQselector.find(".searchBtn").show();
            JQselector.find(".searchPop").hide();
            JQselector.find(".serchInp").focus();
        });
       JQselector.find(".serchInp").blur(function(){
           if($.trim($(this).val()) == ""){
               JQselector.find(".searchBtn").hide();
               JQselector.find(".searchPop").show();
           }

       }).on("input",function(){
           
            var name =$(this).parents().attr('name');
            var keyword=JQselector.find('.serchInp').val(); 
            
           inputSearch(name,keyword)
            //setSearchData(name,keyword);
        });
       
        /*
        $(".searchBtn").click(function(){
            
        });
        */
    }
    function inputSearch(name,keyword){
        var typ = $(".typContent");
        var pro = $(".proContent");
        var fac = $(".facContent");
        var bra = $(".braContent");

        var typId = $('.typInput').attr('data-id');
        var proId = $('.proInput').attr('data-id');
        var facId = $('.facInput').attr('data-id');
        var braId = $('.braInput').attr('data-id');
       
        if (name=='pro') {
            getNameList(typId,keyword, function (data) {
               
                //setValueInSelect(pro, "请选择品名", "pro", "name", data)
                $("#contentCol" + name).html(generateHtml(name,data));
                $("#contentCol" + name).find(".selectRow").click(function(){
                    selectRowFun.apply(this);
                });
            });
        }else if(name=='fac'){
            getFactoryList(typId,proId,keyword, function (data) {
               
                //setValueInSelect(pro, "请选择品名", "pro", "name", data)
                $("#contentCol" + name).html(generateHtml(name,data));
                $("#contentCol" + name).find(".selectRow").click(function(){
                    selectRowFun.apply(this);
                });
            });
        }
    }
    /* end 分类品名厂家牌号四级联动所用的函数*/


});
